package top.so.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.so.dao.BlogArticleDao;
import top.so.dao.BlogCollectionDao;
import top.so.dto.BlogCollectionDTO;
import top.so.entity.BlogArticle;
import top.so.entity.BlogCollection;
import top.so.service.BlogCollectionService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BlogCollectionServiceImpl implements BlogCollectionService {

    @Autowired
    private BlogCollectionDao blogCollectionDao;
    @Autowired
    private BlogArticleDao blogArticleDao;

    public List<BlogCollectionDTO> selectWithPage(BlogCollectionDTO blogCollectionDTO, int pageNum) {
        List<BlogCollection> blogCollectionList = blogCollectionDao.selectWithPage(DTOToPOJO(blogCollectionDTO),pageNum);
        List<BlogCollectionDTO> blogCollectionDTOList = findCollection(blogCollectionList);
        return blogCollectionDTOList;
    }

    private BlogCollection DTOToPOJO(BlogCollectionDTO blogCollectionDTO) {
        BlogCollection blogCollection = new BlogCollection();
        if(blogCollectionDTO!=null){
            blogCollection.setCollectionID(blogCollectionDTO.getCollectionID());
            blogCollection.setArticleID(blogCollectionDTO.getArticleID());
            blogCollection.setCollectionTime(blogCollectionDTO.getCollectionTime());
            blogCollection.setUserID(blogCollectionDTO.getUserID());
        }
        return blogCollection;
    }

    private List<BlogCollectionDTO> findCollection(List<BlogCollection> blogCollectionList ){
        List<BlogCollectionDTO> blogCollectionDTOList=new ArrayList<BlogCollectionDTO>();
        BlogArticle blogArticle;
        BlogCollectionDTO blogCollectionDTO;
        for(BlogCollection blogCollection:blogCollectionList){
            blogArticle = new BlogArticle();
            blogArticle.setArticleID(blogCollection.getArticleID());
            List<BlogArticle> blogArticleList =  blogArticleDao.selectAll(blogArticle);
            if(blogArticleList!=null&&!blogArticleList.isEmpty()){
                blogArticle = blogArticleList.get(0);
            }
           else{
                continue;
            }
            blogCollectionDTO = new BlogCollectionDTO();
            /*************************************************************/
            blogCollectionDTO.setArticleID(blogArticle.getArticleID());
            blogCollectionDTO.setArticleName(blogArticle.getArticleName());
            blogCollectionDTO.setArticleContent(blogArticle.getArticleContent());
            blogCollectionDTO.setCollectionTime(blogCollection.getCollectionTime());
            blogCollectionDTO.setUserID(blogCollection.getUserID());
            /******************************************************************/
            blogCollectionDTOList.add(blogCollectionDTO);
        }
        return blogCollectionDTOList;
    }




    public boolean insertCollection(BlogCollectionDTO blogCollectionDTO) {
        BlogCollection blogCollection = DTOToPOJO(blogCollectionDTO);
        UUID uuid = UUID.randomUUID();
        blogCollection.setCollectionID(String.valueOf(uuid).replaceAll("-",""));
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        blogCollection.setCollectionTime(formatter.format(date));
        if(blogCollectionDao.insertCollection(blogCollection)){
            return true;
        }
        return false;
    }

    public boolean deleteCollection(BlogCollectionDTO blogCollectionDTO) {
        return blogCollectionDao.deleteCollection(DTOToPOJO(blogCollectionDTO));
    }
}
